<?php
/*
	用户端公共文件，访问用户端时默认加载
*/
defined('BY_JYA') or exit('error');
cfc('core')->checklogin();
$_RQ['op']=$_RQ['op']?$_RQ['op']:'index';
if($_RQ['op']=='index'){
	//还不是分销商，跳转申请页
	$return['apply']=$this->get('agent_area',array('mid'=>$_SESSION['uid'],'merchid'=>$_SESSION['onemerchid']));
	$return['member']=$this->get('member',array('mid'=>$_SESSION['uid'],'merchid'=>$_SESSION['onemerchid']));
	if(!$return['member']['mid']){
		$this->model('member')->register();
	}
	$return['become']=$this->setting['become'];
	$return['htmls']=$this->template_url('apply');
	$return['page_setting']=$this->plugin_setting('page');
	if($_SESSION['onemerchid']){
		$return['apply_setting']=$this->plugin_setting('apply'.$_SESSION['onemerchid']);
	}else{
		$return['apply_setting']=$this->plugin_setting('apply');
	}
	$return['item']=model('core/member')->detail($_SESSION['uid'],array('nickname','avatar','realname','mobile','wechat'));	
	$return['item']['oldmobile']=$return['item']['mobile'];
	$setting=uni_setting('register');
	$return['item']['login_type']=$setting['login_type'];
	$return['areas']=$this->model()->available_areas();
	$return['agent_level']=$this->setting['level'];
	set_html('title',$return['apply_setting']['html_title']?:'申请入驻');
	exi($return);
}
if($_RQ['op']=='post'){
	$post=get_postdata();
	if(!$post['agree']){
		exi('请先阅读协议！','error');
	}
	if(!$post['realname']){
		exi('请输入姓名！','error');
	}
	if(!$post['mobile']){
		exi('请输入联系电话！','error');
	}
	if($postdata['login_type']>0 && $post['oldmobile']!=$post['mobile'] && !check_sms_code($post['smscode'])){
		$oldmobile=pdo_getcolumn('core_members',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_SESSION['uid']),'mobile');
		if($post['mobile']!=$oldmobile){
			exi('手机验证码错误！','error');
		}
	}
	$member_update=array(
		'realname'=>trim($post['realname']),
		'mobile'=>trim($post['mobile']),
	);
	model('core/member')->member_update($_SESSION['uid'],$member_update);
	//代理地区整理
	if(!$post['area'] || !$post['area']['province']){
		exi('请选择代理地区！','error');
	}
	$area_code=$this->model()->get_area_code($post['area']);	
	$this->model('member')->apply_agent($_SESSION['uid'],$area_code);
	exi('申请成功！请耐心等待管理员审核！');
}
